using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using PentaBiz.Collections.DataCache;

namespace PentaBiz.Testing.Collections.DataCache
{
    [TestClass]
    public class UnitTestAutoCompleteSource
    {
        [TestMethod]
        [TestCategory("SequentialDataCache")]
        public void TestAutoCompleteLookup()
        {
            var path = "";
            using (var c = SequentialDataCache<AutoCompleteItem>.Initialize(deleteOnClose: true))
            {
                path = c.Path;

                //add 100.000 items 
                for (int i = 0; i < 100000; i++)
                {
                    c.Add(new AutoCompleteItem() { Text = string.Format("{0}Text", i) });
                }

                //query the patterns
                var pattern = "1";
                var items = c.Where(autoCompleteItem => autoCompleteItem.Text.StartsWith(pattern)).ToArray();

            }

            if (File.Exists(path))
                File.Delete(path);
        }

        [Serializable]
        private class AutoCompleteItem
        {
            public string Text { get; set; }
        }
    }

    [TestClass]
    public class UnitTestDictionaryOfItems
    {
        [TestMethod]
        [TestCategory("SequentialDataCache")]
        public void TestIfCanStoreDictionaryOfTreeNode()
        {
            using (var c = SequentialDataCache<TreeNode>.Initialize(deleteOnClose: true))
            {
                //add items to collection
                for (int i = 0; i < 1000; i++)
                {
                    var treeNode = new TreeNode()
                                       {
                                           Word = string.Format("Word{0}", i),
                                           Children = new Dictionary<string, TreeNode>()
                                       };
                    for (int j = 0; j < 100; j++)
                    {
                        var child = new TreeNode() { Word = string.Format("Word{0}", j) };
                        treeNode.Children.Add(string.Format("key{0}{1}", i, j), child);
                    }
                    c.Add(treeNode);
                }

                //assert query
                Assert.AreEqual("Word0", c[0].Word);
                Assert.AreEqual("Word1", c[0].Children["key01"].Word);
                Assert.AreEqual("Word100", c[100].Word);
            }
        }
        [Serializable]
        class TreeNode
        {
            private string word;
            private Dictionary<string, TreeNode> children;

            public string Word
            {
                get { return word; }
                set { word = value; }
            }

            public Dictionary<string, TreeNode> Children
            {
                get { return children; }
                set { children = value; }
            }
        }
    }
}